# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.

# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)

# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
project("grpcapp")

# 移除未使用的代码/数据（分段编译）
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffunction-sections -fdata-sections")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections")
# 关闭 RTTI 和异常（C++ 专用，若项目依赖可删除）
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fno-exceptions")
# # 隐藏符号可见性（默认隐藏，仅导出必要符号）
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=default")
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=default")
# 链接时移除未使用的段（垃圾回收）
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections")
# 链接后 strip 符号（移除冗余符号表，所有模式生效）
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s")  # -s 等价于 strip

get_filename_component(MAIN_PROJECT_DIR
        "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../"
        ABSOLUTE)
if(NOT EXISTS "${MAIN_PROJECT_DIR}/CMakeLists.txt")
    message(FATAL_ERROR "主目录CMakeLists.txt不存在，请检查路径: ${MAIN_PROJECT_DIR}")
endif()

set(BUILD_CLIENT OFF CACHE BOOL "禁用桌面客户端" FORCE)
set(BUILD_SERVER OFF CACHE BOOL "禁用桌面服务端" FORCE)

set(RUN_HAVE_POSIX_REGEX OFF)
set(RUN_HAVE_STEADY_CLOCK OFF)
set(RUN_HAVE_STD_REGEX OFF)
# 不生成protoc二进制文件
set(protobuf_BUILD_PROTOC_BINARIES OFF)
set(gRPC_BUILD_CODEGEN OFF)

set(PROTOBUF_BINARY_DIR ${MAIN_PROJECT_DIR}/build/grpc/third_party/protobuf/)
set(GRPC_BINARY_DIR ${MAIN_PROJECT_DIR}/build/grpc/)
add_subdirectory(${MAIN_PROJECT_DIR} ${CMAKE_CURRENT_BINARY_DIR}/main)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
# is preferred for the same purpose.
#
# In order to load a library into your app from Java/Kotlin, you must call
# System.loadLibrary() and pass the name of the library defined here;
# for GameActivity/NativeActivity derived applications, the same library name must be
# used in the AndroidManifest.xml file.
add_library(${CMAKE_PROJECT_NAME} SHARED
        # List C/C++ source files with relative paths to this CMakeLists.txt.
        native-lib.cpp)

# Specifies libraries CMake should link to your target library. You
# can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries.
target_link_libraries(${CMAKE_PROJECT_NAME}
        # List libraries link to the target library
        android
        log
        server_base)

target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
        ${MAIN_PROJECT_DIR}/src/server/
)